<?php

namespace app\common\service;

use app\common\model\Coupon as CouponModel;
use app\common\model\UserCoupon;

class Coupon extends Base
{
    /**
     * 添加优惠券
     *
     * @param array $data
     * @return array
     */
    public function add($data)
    {
        //过滤数据
        $data = $this->filterData($data);
        //创建优惠券
        $coupon = CouponModel::create($data);
        if (!$coupon) {
            return self::error('提交失败，请重试');
        }

        return self::success($coupon->toArray());
    }

    /**
     * 优惠券赠送给用户
     *
     * @param int $coupon_id 优惠券id
     * @param int $uid 用户id
     * @return array
     */
    public function sendToUser($coupon_id, $uid)
    {
        //查询优惠券的信息
        $coupon = CouponModel::get($coupon_id);

        if (empty($coupon) || $coupon['is_del'] == 1) {
            return self::error('优惠券不存在');
        } elseif ($coupon['status'] != 1) {
            return self::error('优惠券已失效');
        } elseif ($coupon['gain_type'] != 3) {
            return self::error('该券不可赠送');
        }
        list($start_time, $end_time) = CouponModel::explainCouponUseStartEndTime($coupon['use_time_type'], $coupon['use_time_days'], $coupon['use_time_start'], $coupon['use_time_end']);
        if (empty($start_time || empty($end_time))) {
            return self::error('使用期限有误');
        }
        $data = [
            'uid' => $uid,
            'coupon_id' => $coupon_id,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'receive_type' => '系统赠送',
            'coupon_name' => $coupon['coupon_name'],
            'coupon_money' => $coupon['coupon_money'],
            'coupon_type' => $coupon['coupon_type'],
            'cate_ids' => $coupon['cate_ids'],
            'service_ids' => $coupon['service_ids'],
            'use_least' => $coupon['use_least'],
            'use_least_amount' => $coupon['use_least_amount'],
            'gain_time' => time()
        ];
        $userCoupon = UserCoupon::create($data);
        if (!$userCoupon) {
            return self::error('赠送失败，请重试');
        }


        return self::success($userCoupon->toArray());
    }

    /**
     * 过滤数据
     *
     * @param array $data
     * @return array
     */
    private function filterData($data)
    {
        //优惠券类型
        if ($data['coupon_type'] == 1) {
            $data['cate_ids'] = '';
            $data['service_ids'] = '';
        } elseif ($data['coupon_type'] == 2) {
            $data['service_ids'] = '';
        } elseif ($data['coupon_type'] == 3) {
            $data['cate_ids'] = '';
        }

        //发送方式
        if ($data['gain_type'] == 2 || $data['gain_type'] == 3) {
            $data['gain_time_type'] = 0;
            $data['gain_time_start'] = 0;
            $data['gain_time_end'] = 0;
            $data['coupon_num_type'] = 0;
            $data['coupon_total_num'] = 1;
            $data['gain_num_limit'] = 1;
        }

        //使用门槛
        if ($data['use_least'] == 0) {
            $data['use_least_amount'] = 0;
        }

        //领取时间
        if ($data['gain_time_type'] == 0) {
            $data['gain_time_start'] = 0;
            $data['gain_time_end'] = 0;
        }

        return $data;
    }
}
